package servlet;

import bean.Goods;
import bean.Page;
import service.impl.BookServiceImpl;
import utils.DataTypeUtils;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

public class ClientBookServlet extends BaseServlet {
//        默认每页展示数据条数
    private static final Integer BOOK_PAGE_DEFAULT_SIZE = 6;

    private BookServiceImpl bookService = new BookServiceImpl();
//
    private static final String pageUrl = "client/bookServlet?action=page";
    private static final String pageByPriceUrl = "client/bookServlet?action=pageByPrice";


    /**
     * 分页展示全部商品
     */
    protected void page(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println(req.getParameter("pageNumber"));
        int pageNumber = DataTypeUtils.parseInt(req.getParameter("pageNumber"), 1);
        int pageSize = DataTypeUtils.parseInt(req.getParameter("pageSize"), BOOK_PAGE_DEFAULT_SIZE);
        String classifyName = req.getParameter("classify");

        Page<Goods> page = bookService.pagingQuery(pageNumber, pageSize,classifyName);

        page.setUrl(pageUrl);

        req.setAttribute("page", page);

        req.getRequestDispatcher("/pages/client/commodity.jsp").forward(req, resp);

    }



    /**
     * 通过价格查询商品
     */
    protected void pageByPrice(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        System.out.println(req.getParameter("pageNumber"));
        int pageNumber = DataTypeUtils.parseInt(req.getParameter("pageNumber"), 1);
        int pageSize = DataTypeUtils.parseInt(req.getParameter("pageSize"), BOOK_PAGE_DEFAULT_SIZE);
        double minPrice = DataTypeUtils.parseDouble(req.getParameter("min"), 0);
        double maxPrice = DataTypeUtils.parseDouble(req.getParameter("max"), Integer.MAX_VALUE);

        Page<Goods> page = bookService.pagingQueryByPrice(pageNumber, pageSize, minPrice, maxPrice);

        page.setUrl(pageByPriceUrl + "&min=" + minPrice + "&max=" + maxPrice);

        req.setAttribute("page", page);
//        req.setAttribute("pageType","pageByPrice");

        req.getRequestDispatcher("/pages/client/index.jsp").forward(req, resp);

    }
}
